package com.eqdd.yiqidian.ui.discover.makerspace

import android.content.Intent
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.AdvantagedBean
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.ui.base.EditTextActivity
import com.eqdd.yiqidian.utils.TimeUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_advantage_info.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivityForResult

/**
 * Created by jhj on 18-8-3.
 */
class AdvantagedInfoActivity : BaseActivity() {

    private var advantageBean: AdvantagedBean? = null
    private var makerGuid: String? = null


    override fun initBinding() {
        setContentView(R.layout.activity_advantage_info)
        initTopTitleBar(View.VISIBLE, "个人优势")
        makerGuid = intent.getStringExtra(Config.GUID)
    }


    override fun initData() {
        OkGo.post<HttpResult<AdvantagedBean>>(HttpConfig.BASE_URL + HttpConfig.ADVANTAGED_LIST)
                .params("userGuid", makerGuid)
                .execute(object : DialogCallBack<HttpResult<AdvantagedBean>>(this) {
                    override fun onSuccess(response: Response<HttpResult<AdvantagedBean>>?) {
                        val result = response?.body()
                        if (result?.status == 200) {
                            if (result.items != null && result.items.id != 0) { //修改
                                advantageBean = result.items
                                tv_content.text = result.items?.advantage
                                tv_time.text = TimeUtil.formatWebTime1(result.items?.createTime)
                                tv_content.onClick {
                                    startActivityForResult<EditTextActivity>(1001, "title" to "个人优势",
                                            "tip" to "个人优势", "content" to tv_content?.text?.toString())
                                }

                            } else { //新增
                                initTopRightText("新增") {
                                    startActivityForResult<EditTextActivity>(1000, "title" to "个人优势", "tip" to "个人优势")
                                }
                            }
                        }
                    }
                })
    }

    override fun setView() {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Config.INTENT_CONTENT) {
            val str = data?.getStringExtra("content")
            when (requestCode) {
                1000 -> {
                    OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.ADVANTAGED_ADD)
                            .params("userGuid", user.guid)
                            .params("advantage", str)
                            .execute(object : DialogCallBack<HttpResult<*>>(this) {
                                override fun onSuccess(response: Response<HttpResult<*>>?) {
                                    val result = response?.body()
                                    if (result?.status == 200) {
                                        initData()
                                    }
                                }
                            })
                }
                1001 -> {
                    advantageBean?.let {
                        OkGo.post<HttpResult<*>>(HttpConfig.BASE_URL + HttpConfig.ADVANTAGED_UPDATE)
                                .params("userGuid", user.guid)
                                .params("advantage", str)
                                .params("atgId", it.id)
                                .execute(object : DialogCallBack<HttpResult<*>>(this) {
                                    override fun onSuccess(response: Response<HttpResult<*>>?) {
                                        val result = response?.body()
                                        if (result?.status == 200) {
                                            initData()
                                        }
                                    }
                                })
                    }

                }
            }
        }
    }

}